package com.example.maastock.ontime;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.maastock.entity.BookStockOnline;
import com.example.maastock.mapper.BookStockOnlineMapper;
import com.example.maastock.web.FeiShuMessageServiceImpl;
import com.example.maastock.web.StockOnlineObject;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@RequestMapping("/online")
@RestController
public class OnlineController {

    @Autowired
    private SearchOnLine searchOnLine;

    @Autowired
    private EmailService emailService;

    @Autowired
    private BookStockOnlineMapper bookStockOnlineMapper;

    @Autowired
    private FeiShuMessageServiceImpl feiShuMessageService;


    @PostMapping("save-record")
    public Object saveRecord(@RequestBody BookStockOnline bookStockOnline) {
        searchOnLine.saveBooksRecord(bookStockOnline);
        return "OK";
    }


    @GetMapping("/{code}")
    public Object getOnlineCode(@PathVariable String code) {
        Object o = searchOnLine.searchOnlineInTime(code);
        return o;
    }


    @GetMapping("/test/email")
    public Object testEmail() {
        String emailTo = "1332838052@qq.com";
        String subject = "test-email";
        String text = "email-text";
        emailService.sendEmail(emailTo, subject, text,null);
        return "OK";
    }

    @GetMapping("/test/email1")
    public Object testEmail1() {
        QueryWrapper<BookStockOnline> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda().eq(BookStockOnline::getStatus, 1);
        List<BookStockOnline> bookStockOnlines = bookStockOnlineMapper.selectList(queryWrapper);
         return bookStockOnlines;
    }

    @GetMapping("/sendfeishu")
    public Object sendMessageFeiShu(String code) {
        LinkedHashMap<String, String> codeMap = new LinkedHashMap<>();
        codeMap.put("sh000300" ,"沪深300指数");
        codeMap.put("sh000001" ,"上证指数");
        codeMap.put("sz399001" ,"深证指数");
        codeMap.put("sz399006" ,"创业板指数");
        codeMap.put("sz002594", "比亚迪");
        codeMap.put("sz002050", "三花智控");
        codeMap.put("sh600900", "长江电力");
        codeMap.put("sz000733" ,"振华科技");
        codeMap.put("sh601318" ,"中国平安");
        codeMap.put("sh603259" ,"药明康德");
        codeMap.put("sh601899" ,"紫金矿业");
        codeMap.put("sh601607" ,"上海医药");
        codeMap.put("sz000100" ,"TCL科技");
        codeMap.put("sh601985" ,"中国核电");



//        codeMap.put("sz159636" ,"港股通科技30ETF");
//        codeMap.put("sh520660" ,"港股通央企红利etf");
//        codeMap.put("hk01810" , "小米");
        List<String> messageList = new ArrayList<>();
        Boolean bookStatus = false;
        for (Map.Entry<String, String> entry : codeMap.entrySet()) {
            String stockCode = entry.getKey();
            StockOnlineObject stockOnlineObject = (StockOnlineObject) searchOnLine.searchOnlineInTime(stockCode);
            if (ObjectUtil.isEmpty(stockOnlineObject)) {
                continue;
            }
            if (! bookStatus){
                messageList.add("订阅时间 : " +stockOnlineObject.getNowTime() );
                bookStatus = true;

            }

            String message = null;
//            Double goldenPrice = stockOnlineObject.getGoldenPrice();
//            Boolean isGold = false;
//            if (ObjectUtil.isEmpty(goldenPrice)){
//                isGold = goldenPrice.compareTo(Double.valueOf(stockOnlineObject.getNowPrice()) )  > 0 ;
//            }

//            BigDecimal bigDecimal = calculateChangePercentage(new BigDecimal(stockOnlineObject.getFirstPrice()), new BigDecimal(stockOnlineObject.getNowPrice()));
//            message = "股票:" + entry.getValue() + " 开盘价:" + stockOnlineObject.getFirstPrice() + " 现价:" + stockOnlineObject.getNowPrice() + " 时间：" + stockOnlineObject.getNowTime()
//            +" 涨跌幅:" + bigDecimal.toString()+"%"+ "现价是否在金叉位置(金叉是指 MACD 的 DIF 线从下向上穿过 DEA 线的交): "  + isGold   ;


            BigDecimal bigDecimal = calculateChangePercentage(new BigDecimal(stockOnlineObject.getFirstPrice()), new BigDecimal(stockOnlineObject.getNowPrice()));
            message = "股票:" + entry.getValue() + " 开盘价:" + stockOnlineObject.getFirstPrice() + " 现价:" + stockOnlineObject.getNowPrice() +
                    " 涨跌幅:" + bigDecimal.toString()+"%";

            messageList.add(message);
        }
        StringBuilder mess = new StringBuilder();
        for (String messageDetail : messageList) {
            mess.append(messageDetail).append(System.lineSeparator());
//            feiShuMessageService.sendMessage(messageDetail);
        }

        feiShuMessageService.sendMessage(mess.toString());
        return true;
    }

    public  BigDecimal calculateChangePercentage(BigDecimal openingPrice, BigDecimal currentPrice) {
        BigDecimal subtract = currentPrice.subtract(openingPrice);
        BigDecimal divide = subtract.divide(openingPrice , 4 , RoundingMode.HALF_UP );
        BigDecimal multiply = divide.multiply(new BigDecimal(100));
        return multiply;

    }

}
